package com.example.a10928.weifriend.ui.task_center;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.a10928.weifriend.R;
import com.example.a10928.weifriend.adapter.MemberTaskAdapter;
import com.example.a10928.weifriend.databinding.FragmentTaskCenterBinding;
import com.example.a10928.weifriend.databinding.FragmentTaskRecordBinding;
import com.example.a10928.weifriend.http.api.base.ImageViewInfo;
import com.example.a10928.weifriend.http.api.base.MyAdverBean;
import com.example.a10928.weifriend.ui.BaseFragment;
import com.example.a10928.weifriend.util.RxUtil;
import com.previewlibrary.GPreviewBuilder;

import java.util.ArrayList;
import java.util.List;

import static com.example.a10928.weifriend.App.activity;
import static com.example.a10928.weifriend.App.app;

@SuppressLint("ValidFragment")
public class TaskRecordFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private int state = 0;
    private String data_type = "";
    private FragmentTaskRecordBinding fb;


    public TaskRecordFragment(int state) {
        this.state = state;
    }

    public TaskRecordFragment(int state, String data) {
        this.state = state;
        this.data_type = data;
    }

    List<MemberTaskAdapter.TaskItemData> data = new ArrayList();
    MemberTaskAdapter adapter;
    SwipeRefreshLayout swipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fb = DataBindingUtil.inflate(inflater, R.layout.fragment_task_record, container, false);
        adapter = new MemberTaskAdapter(R.layout.item_member_task, data, data_type);
        fb.taskRecordRv.setAdapter(adapter);
        fb.taskRecordRv.setLayoutManager(new LinearLayoutManager(getContext()));
        swipeRefreshLayout = fb.getRoot().findViewById(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(this);
        initData();
        return fb.getRoot();
    }

    @SuppressLint("CheckResult")
    private void initData() {
        ((TaskRecordActivity) getActivity()).closeProgress();
        ((TaskRecordActivity) getActivity()).openProgress();
        app().getBpService().getMyTask(app().getUser().getUserId(), state)
                .compose(RxUtil.io2main())
                .subscribe(
                        r -> {
                            swipeRefreshLayout.setRefreshing(false);
                            data.clear();
                            for (MyAdverBean item : r.getData()) {
                                data.add(new MemberTaskAdapter.TaskItemData(
                                        R.drawable.task_vip,
                                        item.getAd_title(),
                                        item.getCreated_at(),
                                        item.getCommission(),
                                        TaskDetailsActivity.Companion.getSOURCE_SEND_THE_TASK(),
                                        item.getId(),
                                        false,
                                        item.getIs_finish() == 3 ? true : false));
                            }
                            if (state == 0 && data.size() != 0) {
                                fb.taskRecordImg.setVisibility(View.VISIBLE);
                                fb.taskRecordImg.setOnClickListener(v -> {
                                    String url = "http://yy1i8.cn/app/task_help.png";
                                    GPreviewBuilder.from(getActivity())
                                            .setSingleData(new ImageViewInfo(url))
                                            .setCurrentIndex(0)
                                            .setUserFragment(ImageShowFragment.class)
                                            .setType(GPreviewBuilder.IndicatorType.Dot)
                                            .start();
                                });

                            }
                            adapter.setNewData(data);
                            ((TaskRecordActivity) getActivity()).closeProgress();
                        },
                        RxUtil::errorException
                );
    }


    @Override
    public void onRefresh() {
        initData();
    }
}
